/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                           *
 *                          Copyright (c) 2010                               *
 *                  Alen Stojanov [ ioi.alen@gmail.com ]                     *
 *                Jacobs University Bremen, Bremen, Germany                  *
 *                                                                           *
 *                          All Rights Reserved.                             *
 *                                                                           *
 *    Permission to use, copy, modify and distribute this software and       *
 *    its documentation is hereby granted, provided that both the            *
 *    copyright notice and this permission notice appear in all copies of    *
 *    the software, derivative works or modified versions, and any           *
 *    portions thereof, and that both notices appear in supporting           *
 *    documentation.                                                         *
 *                                                                           *
 *    This code is distributed in the hope that it will be useful, but       *
 *    WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS       *
 *    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER          *
 *    RESULTING FROM THE USE OF THIS SOFTWARE.                               *
 *                                                                           *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <QMdiSubWindow>
#include <QFileDialog>
#include <QMessageBox>
#include <QIcon>
#include <QDir>
#include <QStyle>
#include "qticonloader.h"

#include "tsrenderer.h"
#include "tsview.h"
#include "tsabout.h"

#include <iostream>

using namespace std;

TSRenderer::TSRenderer(QWidget *parent)
: QMainWindow(parent)
  {
	/* setup the main UI */
	ui.setupUi(this);

	/* make the mdiArea central */
	this->setCentralWidget(ui.tsViewsArea);

#ifndef Q_OS_WIN32
	/* X11, MAC OS X & other conform to freedesktop stanards */
	QIcon iconNew    = QtIconLoader::icon("document-new");
	QIcon iconOpen   = QtIconLoader::icon("document-open");
	QIcon iconSave   = QtIconLoader::icon("document-save");
	QIcon iconSaveAs = QtIconLoader::icon("document-save-as");
	QIcon iconClose  = QtIconLoader::icon("dialog-close");
#else
	/* qticonloader does not support windows */
	QIcon iconNew    = style()->standardIcon(QStyle::SP_FileDialogDetailedView);
	QIcon iconOpen   = style()->standardIcon(QStyle::SP_DialogOpenButton);
	QIcon iconSave   = style()->standardIcon(QStyle::SP_DialogSaveButton);
	QIcon iconSaveAs = style()->standardIcon(QStyle::SP_DialogSaveButton);
	QIcon iconClose  = style()->standardIcon(QStyle::SP_DialogCloseButton);
#endif
	QIcon iconImport; iconImport.addFile(QString::fromUtf8(":/icons/icons/1283682239_fileimport.png"), QSize(), QIcon::Normal, QIcon::Off);

	/* include actions in the tool box */
	actionMenuNew     = ui.mainToolBar->addAction(iconNew,     "New" ,    this, SLOT(on_actionNew_triggered()) );
	actionMenuOpen    = ui.mainToolBar->addAction(iconOpen,    "Open",    this, SLOT(on_actionOpen_triggered()));
	ui.mainToolBar->addSeparator();
	actionMenuSave    = ui.mainToolBar->addAction(iconSave,    "Save",    this, SLOT(on_actionSave_triggered()));
	actionMenuSaveAs  = ui.mainToolBar->addAction(iconSaveAs,  "Save As", this, SLOT(on_actionSaveAs_triggered()));
	ui.mainToolBar->addSeparator();
	actionMenuClose   = ui.mainToolBar->addAction(iconClose,   "Close",   this, SLOT(on_actionClose_triggered()));
	ui.mainToolBar->addSeparator();
	actionMenuImport  = ui.mainToolBar->addAction(iconImport,  "Import",  this, SLOT(on_actionImport_triggered()));

	/* set standard icons */
	ui.actionNew->setIcon     ( iconNew    );
	ui.actionOpen->setIcon    ( iconOpen   );
	ui.actionSave->setIcon    ( iconSave   );
	ui.actionSaveAs->setIcon  ( iconSaveAs );
	ui.actionClose->setIcon   ( iconClose  );
	ui.actionImport->setIcon  ( iconImport );

	/* disable the non-logical actions */
	actionMenuSave->setDisabled(true);
	actionMenuSaveAs->setDisabled(true);
	actionMenuClose->setDisabled(true);
	actionMenuImport->setDisabled(true);
	ui.actionSave->setDisabled(true);
	ui.actionSaveAs->setDisabled(true);
	ui.actionClose->setDisabled(true);
	ui.actionImport->setDisabled(true);

	// connect(ui.mainToolBar, SIGNAL(visibilityChanged(bool)),
		//	this, SLOT(setChecked(bool)));

	this->showMaximized();
}

void TSRenderer::on_actionNew_triggered ()
{
	QString fileList;
	TSView* view = new TSView(this, fileList);
	QMdiSubWindow *subWindow = new QMdiSubWindow;
	subWindow->setWidget(view);
	subWindow->setAttribute(Qt::WA_DeleteOnClose);
	ui.tsViewsArea->addSubWindow(subWindow);
	view->showMaximized();
}

void TSRenderer::on_actionOpen_triggered ()
{
	QStringList fileList = QFileDialog::getOpenFileNames(
			this,
			tr("Open Time Series Data Set"),
			QDir::current().absolutePath(),
			tr("Time Series Renderer (*.tsr)")
	);
	if ( fileList.size() > 0 ) {
		TSView* view = new TSView(this, "");
		QMdiSubWindow *subWindow = new QMdiSubWindow;
		subWindow->setWidget(view);
		subWindow->setAttribute(Qt::WA_DeleteOnClose);
		ui.tsViewsArea->addSubWindow(subWindow);
		view->show();
	}
}

void TSRenderer::on_actionSave_triggered ()
{

}

void TSRenderer::on_actionSaveAs_triggered ()
{

}

void TSRenderer::on_actionClose_triggered ()
{

}

void TSRenderer::on_actionImport_triggered ()
{

}

void TSRenderer::on_actionToggleMainToolbox_triggered ()
{
	if ( ui.mainToolBar->isVisible() ) {
		ui.mainToolBar->setVisible(false);
		ui.actionToggleMainToolbox->setChecked(false);
	} else {
		ui.mainToolBar->setVisible(true);
		ui.actionToggleMainToolbox->setChecked(true);
	}
}

void TSRenderer::on_actionToggleGraphToolbox_triggered ()
{
	if ( ui.toolBarLeft->isVisible() ) {
		ui.toolBarLeft->setVisible(false);
		ui.actionToggleGraphToolbox->setChecked(false);
	} else {
		ui.toolBarLeft->setVisible(true);
		ui.actionToggleGraphToolbox->setChecked(true);
	}
}

void TSRenderer::on_toolBarLeft_visibilityChanged(bool visible)
{
	ui.actionToggleGraphToolbox->setChecked(visible);
}


void TSRenderer::on_actionTabs_triggered ()
{
	ui.tsViewsArea->setViewMode(QMdiArea::TabbedView);
	ui.actionWindows->setChecked(false);
}

void TSRenderer::on_actionWindows_triggered ()
{
	ui.tsViewsArea->setViewMode(QMdiArea::SubWindowView);
	ui.actionTabs->setChecked(false);
}

void TSRenderer::on_actionAbout_triggered ()
{
	TSAbout *about = new TSAbout();
	about->show();
}

void TSRenderer::on_cmdLineGraph_clicked()
{
	QMdiSubWindow *subWindow = ui.tsViewsArea->activeSubWindow();
	TSView* tsview = (TSView*)subWindow->widget();
	if ( tsview != NULL ) {
		tsview->toggleLineGraph();
	}
}

void TSRenderer::on_cmdLSPGraph_clicked()
{
	QMdiSubWindow *subWindow = ui.tsViewsArea->activeSubWindow();
	TSView* tsview = (TSView*)subWindow->widget();
	if ( tsview != NULL ) {
		tsview->toggleLSPGraph();
	}
}

void TSRenderer::on_cmdPreviewGraph_clicked()
{
	QMdiSubWindow *subWindow = ui.tsViewsArea->activeSubWindow();
	TSView* tsview = (TSView*)subWindow->widget();
	if ( tsview != NULL ) {
		tsview->togglePreviewGraph();
	}
}

void TSRenderer::on_cmdSettings_clicked()
{
	QMdiSubWindow *subWindow = ui.tsViewsArea->activeSubWindow();
	TSView* tsview = (TSView*)subWindow->widget();
	if ( tsview != NULL ) {
		tsview->toggleSettings();
	}
}

TSRenderer::~TSRenderer()
{
	// Do nothing
}
